<?php

namespace App\Http\Controllers\Common;

use App\Helpers\Code;
use App\Models\Common\DataScope;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class DataScopeController extends Controller
{

    public function __construct()
    {
        $this->model = new DataScope();

        $this->_validators();
    }

    /**
     * 添加数据域
     * @param Request $request
     * @return array
     */
    public function addScope(Request $request)
    {
        try {
            $data = $request->only(['parent_id', 'title', 'code', 'description']);

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addScope($data);

            $this->checked($model, ['null' => 'scope.parent_id__not_exists']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 删除数据域
     * @param $id
     * @return array
     */
    public function deleteScope($id)
    {
        try {
            $model = $this->model->deleteScope($id);

            $this->checked($model, [
                'null' => 'common.recored_not_found',
                Code::INSIDE_ERROR => 'scope.scope_have_child',
                Code::INSIDE_ERROR_1 => 'scope.scope_have_user',
                Code::INSIDE_ERROR_2 => 'scope.scope_menu_exists'
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑数据域
     * @param $id
     * @param Request $request
     * @return array
     */
    public function editScope($id, Request $request)
    {
        try {
            $data = $request->only(['parent_id', 'title', 'code', 'description']);

            if (isset($data['code'])){
                $this->rules['code'] = array('bail', 'required', 'max:100', \Illuminate\Validation\Rule::unique('data_scopes')->ignore($id));
            }

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            if (!$request->has('parent_id')) {
                $data['parent_id'] = 'flag';
            }

            $model = $this->model->editScope($id, $data);

            $this->checked($model, [
                'null' => 'common.recored_not_found',
                Code::INSIDE_ERROR => 'scope.not_set_self_to_parent',
                Code::INSIDE_ERROR_1 => 'scope.parent_not_exists',
                Code::INSIDE_ERROR_2 => 'scope.not_set_self_child_to_parent',
                Code::INSIDE_DATABASE_ERROR => 'common.database_error'
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取数据域全部
     * @return array
     */
    public function getScopeListAll()
    {
        try {
            $model = $this->model->getScopeListAll();

            $this->result['data'] = $model;

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取数据域详情
     * @param $id
     * @return array
     */
    public function getScope($id)
    {
        try {
            $model = $this->model->getScope($id);

            $this->checked($model, ['null' => 'common.get_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取数据域列表
     * @return array
     */
    public function getScopeList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);

            $sorter = $this->handleSort($request, $this->model->sorters);

            $this->result['data'] = $this->model->getScopeList($sorter, $search);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function getScopeUsers($id, Request $request)
    {
        try {
            $limit = (int) $request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            $model = $this->model->getScopeUsers($id, $limit, $sorter);

            if ($model === null) {
                return $this->error('common.get_failure');
            }

            return preprocess($model);
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'parent_id' => 'bail|nullable|numeric',
            'title' => 'bail|required|max:50',
            'code' => 'bail|required|max:100|unique:data_scopes',
            'description' => 'bail|nullable|max:250',
        ];

        $this->messages = [
            'parent_id.numeric' => trans('scope.parent_id_numeric'),
            'title.required' => trans('scope.title_required'),
            'title.max' => trans('scope.title_max'),
            'code.required' => trans('scope.code_required'),
            'code.max' => trans('scope.code_max'),
            'code.unique' => trans('scope.code_unique'),
            'description.max' => trans('scope.description_max'),
        ];
    }
}
